package com.eascs.admin.o2o.applyAssessment.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.applyAssessment.entity.ApplyAssessmentEntity;
import com.eascs.admin.o2o.applyAssessment.entity.ApplyAssessmentVo;
import com.eascs.admin.o2o.applyAssessment.service.ApplyAssessmentService;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.type.SystemModule;
import com.eascs.workflow.common.vo.ModelResult;

@Controller
@RequestMapping("/applyAssessment")
public class ApplyAssessmentController extends BaseMasterViewController {
	private final Logger logger = LoggerFactory.getLogger(ApplyAssessmentController.class);

	@Reference
	SysPermissionService sysPermissionService;
	@Reference
	SysOrgService sysOrgService;
	// @Autowired
	// private BaseUtils baseUtils;
	@Autowired
	private ApplyAssessmentService service;
	@Autowired
	private BaseUtils baseUtils;

	/**
	 * @author LQ
	 * @date 2018年2月28日
	 * @return
	 */
	@RequestMapping("/list")
	public String list(ApplyAssessmentVo vo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "applyAssessment");
		vo.setPageSize(10);
		String url = "/applyAssessment/list";
		String name = vo.getName() == null ? "" : vo.getName();
		String position = vo.getPosition() == null ? "" : vo.getPosition();
		String hiredateStart = vo.getHiredateStart() == null ? "" : vo.getHiredateStart();
		String hiredateEnd = vo.getHiredateEnd() == null ? "" : vo.getHiredateEnd();
		String provinceCode = vo.getProvinceCode() == null ? "" : vo.getProvinceCode();// 数据字典中选出来的是province
																						// code
		url = UrlUtil.appendUrl(url, "name", name);
		url = UrlUtil.appendUrl(url, "position", position);
		url = UrlUtil.appendUrl(url, "hiredateStart", hiredateStart);
		url = UrlUtil.appendUrl(url, "hiredateEnd", hiredateEnd);
		url = UrlUtil.appendUrl(url, "provinceCode", provinceCode);
		url = url + "&pageIndex=";
		List<User> users = sysOrgService.checkDataPermission(UserHolder.getUser().getNo());
		String no = "";
		if (users != null) {
			for (User user : users) {
				no += user.getNo() + ",";
			}
		}
		if (!"".equals(no)) {
			if (",".equals(no.substring(no.length() - 1, no.length()))) {
				no = no.substring(0, no.length() - 1);
			}
		}
		vo.setUsersno(no);
		PageResult<Object[]> pageResult = service.getApplyAssessmentByPage(vo);
		String titles[] = { "流程单号", "姓名", "省区", "职位", "入职时间", "所属团队", "状态" };
		SelectDataOption[] selectDataOptions = { null, null, null, new SelectDataOption("POSITION_MANAGER_DIRECTOR"),
				null, null, new SelectDataOption("parWFStutus") };// 需要职位数据字典
		Button[] titleButtons = new Button[] { ButtonHelper.createOpenButton(null, "/applyAssessment/detail?id=") };// 1)以弹窗的形式打开(ajax按钮放在查询列表第一列)
		// Button[] titleButtons = new Button[] { null,ButtonHelper.createOpenButton(null, "/applyAssessment/detail?id=") };// (此写法为ajax按钮放在查询列表第二列)
		// delete
		Button delete = ButtonHelper.createAjaxConfirmButton("删除", "/rApplyAssessment/delete?id=", "delApply");
		delete.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				Object wfstate = params[7];
				if ("-1".equals(wfstate + "") || "3".equals(wfstate + ""))// 新建和驳回才显示删除按钮
					return true;
				else
					return false;
			}
		});
		// 发起流程--------------------------------------------------------------------------------permissionCode---
		Button startFlow = ButtonHelper.createAjaxButton("启动流程", "/rApplyAssessment/applyStartFlow?id=",
				"startApplyFlow");// parameter为合作方id
		startFlow.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				Object wfstate = params[7];
				if ("-1".equals(wfstate + "") || "3".equals(wfstate + ""))
					return true;
				else
					return false;
			}
		});

		// 编辑
		Button edit = ButtonHelper.createAjaxButton("编辑", "/rApplyAssessment/edit?id=", "editApply");// parameter为合作方id
//		User user = UserHolder.getUser();
		// edit.setShowButtonFilter(new ShowButtonFilter() {
		// @Override
		// public boolean filter(Object param) {
		// Object[] params = (Object[]) param;
		// Object wfstate = params[6];
		// Object operatorId = params[7];
		// if ("-1".equals(wfstate + "") || "3".equals(wfstate + "")) {
		// if (user.getNo().equals(operatorId + "") ||
		// user.isSuperAdministrator()) {
		// return true;
		// }
		// } else if ("0".equals(wfstate + "")) {
		// ModelResult<Boolean> result =
		// sysPermissionService.checkAuth(user.getNo(), "ZBPPSH");//总部品牌审核
		// if (result.isSuccess() && result.getModel()) {
		// return true;
		// }
		// }
		// return false;
		// }
		// });
		edit.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				Object wfstate = params[7];
				if ("-1".equals(wfstate + "") || "3".equals(wfstate + ""))
					return true;
				else
					return false;
			}
		});
		Button[] morButtons = new Button[] { delete, startFlow, edit };

		PaginationInfo pagingInfo = new PaginationInfo(pageResult, titles, url);
		pagingInfo.setMoreButtons(morButtons);
		pagingInfo.setTitleButtons(titleButtons);
		pagingInfo.setBuildPagingNavigation(true);
		pagingInfo.setSelectDataOptions(selectDataOptions);
		pagingInfo.setFirstRenderColumnIndex(1);
		List<ActOrgEntity> orgProvince = sysOrgService.getOrgProvince();// 省区
		ViewContext.put("ActOrgEntitys", orgProvince);
		ViewContext.put("vo", vo);
		ViewContext.put("Status", vo.getStatus());
		ViewContext.put("paginationInfo", pagingInfo);
		return "/applyAssessment/list";
	}

	/**
	 * 详情展示
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/detail")
	public String detailInfo(String id) {
		ApplyAssessmentEntity applyEntity = service.getEntityById(id);
		ViewContext.put("applyEntity", applyEntity);
		String agreeUrl = baseUtils.getApprovalAgreeUrl(id, null, SystemModule.SYSTEM_MODULE_O2O);
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("billCode_id", id);
		
//		User user = UserHolder.getUser();
//		int wfStatus = applyEntity.getWfStatus(); //流程状态
//		ModelResult<Boolean> result = sysPermissionService.checkAuth(user.getNo(), "ZBPPSH");// 总部品牌审核
//		if ("-1".equals(wfStatus + "") || "-3".equals(wfStatus + "")) {
//			if (user.getNo().equals(wfStatus) || user.isSuperAdministrator()) {
//				ViewContext.put("isEdit", "isEdit");
//			}
//		} else if ("0".equals(wfStatus + "")) {
//			if (result.isSuccess() && result.getModel()) {
//				ViewContext.put("isEdit", "isEdit");
//			}
//		}
		return "/applyAssessment/apply_detail";
	}

}
